package com.fanjia.demologin.controller;


import com.fanjia.demologin.entity.Sms;
import com.fanjia.demologin.entity.UserTest;
import com.fanjia.demologin.service.SmsService;
import com.fanjia.demologin.service.UserTestService;
import com.fanjia.demologin.util.ResultUtil;
import com.fanjia.demologin.util.SmsUtils;
import com.github.qcloudsms.httpclient.HTTPException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.xml.transform.Result;
import java.io.IOException;

/**
 * 用户表(UserTest)表控制层
 *
 * @author fanjia
 * @since 2021-03-10 15:17:30
 */
@RestController
@RequestMapping("/page")
public class UserTestController {

    @Resource
    private UserTestService userTestService;

    @Autowired
    SmsService smsService;


    /**
     * @author fanjia
     * @description 4注册
     * @date  2021/3/11 14:20
     * @param userTest
     * @return com.fanjia.demologin.util.ResultUtil
     **/
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public ResultUtil register(UserTest userTest) {

        boolean checkIsRegister = userTestService.insert(userTest);
        //System.out.println(checkIsRegister);
        return ResultUtil.success("用户注册成功");
    }




    /**
     * @author fanjia
     * @description 2.1发送验证码
     * @date  2021/3/11 10:13
     * @param userTest
     * @return com.fanjia.demologin.util.ResultUtil
     **/
    @RequestMapping(value = "/loginMode2/sendCode", method = RequestMethod.POST)
    public ResultUtil verifyPhone(UserTest userTest,Sms sms) throws HTTPException, IOException {
        //验证手机号是否正确
        boolean phoneIsExist = userTestService.phoneIsExist(userTest.getPhone());
        if(phoneIsExist){
            //生成验证码
            String code = smsService.generateAuthCode(sms.getPhone());
            System.out.println(code);
            sms.setCode(code);
            //手机号发送验证码
            SmsUtils.send(sms);

            return ResultUtil.success("发送验证码成功");
        }


        return ResultUtil.error("该手机号无登陆权限，请重新输入！");

    }


    /**
     * @author fanjia
     * @description 2.2登录接口
     * @date  2021/3/11 10:13
     * @param
     * @return com.fanjia.demologin.util.ResultUtil
     **/
    @RequestMapping(value = "/loginMode2/login", method = RequestMethod.POST)
    public ResultUtil login2(Sms sms) {

        String code = "1234";
        boolean phoneIsExist = userTestService.phoneIsExist(sms.getPhone());//判断手机号是否正确
        if(phoneIsExist){
            if(code.equals(sms.getCode())){
                return ResultUtil.success("登录成功");
            }

            return ResultUtil.error("验证码输入错误");
        }

        return ResultUtil.error("手机号错误");
    }


    /**
     * @param userTest
     * @return com.tianji.tingcloudapi.common.utils.ResultUtil
     * @author fanjia
     * @description 3.1云图账号密码登录
     * @date 2021/3/10 15:27
     **/
    @RequestMapping(value = "/loginMode3/login", method = RequestMethod.POST)
    public ResultUtil login3(UserTest userTest) {


        boolean userExist = userTestService.isUserExist(userTest);

        if (userExist) {
            return ResultUtil.success("登录成功");
        }
        return ResultUtil.error("用户名或密码错误");

    }


    //3.2 忘记密码1，验证手机号和账号,验证码是否正确
    @RequestMapping(value = "/loginMode3/verifyAccount", method = RequestMethod.POST)
    public ResultUtil verifyAccount(UserTest userTest,Sms sms ) {

        System.out.println(userTest);
        System.out.println(sms);
        String code = "1234";
        //手机号是否存在
        boolean phoneWhether = userTestService.phoneIsExist(userTest.getPhone());
        System.out.println(phoneWhether);
        if(phoneWhether){
            //根据手机号验证账号是否正确
           boolean checkAccountExist = userTestService.checkAccount(userTest);
           if(checkAccountExist){
               if(code.equals(sms.getCode())){
                   return ResultUtil.success("跳入忘记密码2页面");
               }
               return ResultUtil.error("验证码错误");

           }
        }


        return ResultUtil.error("手机号与账户不匹配");
    }




    /**
     * @author fanjia
     * @description 3.4忘记密码2，重置密码信息
     * @date  2021/3/11 9:55
     * @param userTest
     * @return com.fanjia.demologin.util.ResultUtil
     **/
    @RequestMapping(value = "/loginMode3/resetPassword", method = RequestMethod.POST)
    public ResultUtil resetPassword(UserTest userTest) {
        //根据手机号 重新设置密码
        boolean idReset = userTestService.updatePasswordByPhone(userTest);
        System.out.println(idReset);
        if(idReset){
            return ResultUtil.success("重置密码成功");
        }
        return ResultUtil.error("重置密码失败");
    }












































}